package com.rzico.core.plugin.msg;

import com.alibaba.fastjson.JSON;
import com.rzico.core.entity.SysPlugin;
import com.rzico.core.model.PluginAttribute;
import com.rzico.core.plugin.MsgPlugin;
import com.rzico.util.DateUtils;
import com.rzico.util.HttpUtils;
import com.rzico.util.MD5Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;


/**
 * Plugin -  豆讯短信
 *
 * @author RZICO.BOOT
 * @version 3.0
 */
@Component("douxunPlugin")
public class DouxunPlugin  extends MsgPlugin {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public String getName() {
        return "豆讯验证码/友盟推送";
    }

    @Override
    public String getVersion() {
        return "1.00";
    }

    @Override
    public List<PluginAttribute> getAttributeKeys() {
        List<PluginAttribute> data = new ArrayList<>();
        data.add(new PluginAttribute("custInteId","custInteId",PluginAttribute.STRING_VALUE));
        data.add(new PluginAttribute("secretKey","secretKey",PluginAttribute.STRING_VALUE));
        data.add(new PluginAttribute("umengAndroidAppkey","androidAppkey",PluginAttribute.STRING_VALUE));
        data.add(new PluginAttribute("umengAndroidMasterSecret","androidMasterSecret",PluginAttribute.STRING_VALUE));
        data.add(new PluginAttribute("umengIOSAppkey","IOSAppkey",PluginAttribute.STRING_VALUE));
        data.add(new PluginAttribute("umengIOSMasterSecret","IOSMasterSecret",PluginAttribute.STRING_VALUE));
        return data;
    }

    /**
     * 发送短信
     * @param sysPlugin
     * @param mobiles
     * @param content
     * @throws IOException
     */
    @Override
    public void sendMsg(SysPlugin sysPlugin, String mobiles, String content) throws IOException {
        String timestamp = DateUtils.getTimeStr(new Date(), DateUtils.DB_STORE_DATE);
        //map转json
        Map<String,Object> params = new HashMap<>();
        params.put("custInteId", sysPlugin.getAttribute("custInteId"));
        params.put("mobile", mobiles);
        params.put("content", content);
        params.put("timestamp", timestamp);
        params.put("signKey", MD5Utils.getMD5Str(sysPlugin.getAttribute("custInteId") + mobiles
                + sysPlugin.getAttribute("secretKey") + timestamp).toUpperCase());
        String response = HttpUtils.httpGet("http://sapi.juhemall.cn/order.action", params);
        Map result = JSON.parseObject(response, Map.class);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        for (Object obj : result.keySet()) {
            resultMap.put(obj.toString(), result.get(obj));
        }
        if ("0000".equals(resultMap.get("result").toString())){
            logger.info("成功");
        } else {
            logger.info("失败");
        }
    }


}
